# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import datetime
import re

from pymongo import MongoClient


class SmzdmPipeline(object):

    def __init__(self):
        self.client = MongoClient("127.0.0.1", 27017)

        # 数据库名字和集合名字，没有会自动创建
        self.collection = self.client["exam1"]["data"]

    def process_item(self, item, spider):
        price_info = item.get("price_info")
        price_info = price_info.replace("元", "")

        isbao = ""
        if "包邮" in price_info:
            isbao = "包邮"

            price_info = price_info.replace("包邮", "")

        beizhu = ""
        if "（" in price_info and "）" in price_info:
            a = price_info.find("（")
            b = price_info.find("）")
            beizhu = price_info[a + 1:b]
            price_info = price_info.replace("（%s）" % beizhu, "")

        time = item.get("time")
        if time.find(":") == -1:
            yue, ri = time.split("-")
            time = "2020年%s月%s日" % (yue, ri)
        else:

            time = datetime.datetime.now().strftime('%Y年%m月%d日')

        price = re.findall(r'[0-9]+', price_info)[0]

        # 写入数据库

        self.collection.insert({
            "title": item.get("title"),
            "price": price,
            "beizhu": beizhu,
            "isbao": isbao,
            "time": time
        })

        print("写入数据库成功")

        return item

    def __del__(self):
        self.client.close()
